package com.xd.demo.process.impl;

import com.alibaba.fastjson.JSON;
import com.xd.demo.dto.*;
import com.xd.demo.process.ReqResProcessor;
import com.xd.demo.util.AesUtils;
import com.xd.demo.xydto.XyResDto;
import org.apache.tomcat.util.codec.binary.Base64;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author fanrui
 * @date 2020/9/2
 */
public class ReqResProcessorJdImpl extends ReqResProcessor {

    /**
     * @param date 京东传入日期格式
     * @return 时间毫秒值
     * @throws ParseException
     */
    public long translateDateJdToXy(String date) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        Date time = sdf.parse(date);
        return time.getTime();
    }


    /**
     * @param timeMillis 时间毫秒值
     * @return JD的时间格式
     */
    public String translateDateXyToJd(long timeMillis) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        Date date = new Date(timeMillis);
        return sdf.format(date);
    }

    @Override
    public boolean supports(String reqBody) {

        // 进行类型转换判断渠道是否支持
        try {
            JdReqDto jdReqDto = JSON.parseObject(reqBody, JdReqDto.class);
        } catch (Exception e) {
            return false;
        }

        return true;
    }

    @Override
    public String reqBodyProcess(String reqBody) throws Exception {
        JdReqDto jdReqDto = JSON.parseObject(reqBody, JdReqDto.class);
        // 验签
//        boolean result = AesUtils.sign(jdReqDto.getRequestData(), "salt").equals(jdReqDto.getSysSign());
//        if (!result) {
//            // FIXME 自定义异常
//            throw new Exception();
//        } else {
//            // FIXME 自定义异常 解密
//            String data = AesUtils.decryptAES("key", jdReqDto.getRequestData());
//            //
//            // FIXME 泛型
//            ReqDto reqDto = null;
//            if(jdReqDto.getTxCode().equals("CREDIT_APPLY")){
//                // 授信申请
//                reqDto = new ReqDto<JdCreditApplyResDto>();
//                JdCreditApplyResDto jdCreditApplyResDto = JSON.parseObject(data,JdCreditApplyResDto.class);
//                reqDto.setData(jdCreditApplyResDto);
//            }else{
//                // 查询结果
//            }
            // ReqDto--属性赋值
            ReqDto reqDto = null;
            reqDto = new ReqDto<JdCreditApplyReqDto>();
            JdCreditApplyReqDto jdCreditApplyResDto = JSON.parseObject(jdReqDto.getRequestData(),JdCreditApplyReqDto.class);
            reqDto.setData(jdCreditApplyResDto);
            return JSON.toJSONString(reqDto);
        }



    @Override
    public String resBodyProcess(String resBody) throws Exception {
//        XyResDto xyResDto = JSON.parseObject(resBody, XyResDto.class);
        JdResDto jdResDto1 = JSON.parseObject(resBody, JdResDto.class);
//        JdResDto jdResDto = new JdResDto();
//        jdResDto.setResponseNo(xyResDto.getRequestKey().substring(3));
//        jdResDto.setResponseTime(translateDateXyToJd(xyResDto.getTimestamp()));
//        jdResDto.setCode(JdResDto.SUCCEED);
//        jdResDto.setMsg("");
        // 数据加密
        String data = AesUtils.encryptAES("1234561234561234", jdResDto1.getResponseData());
        jdResDto1.setResponseData(data);
        // 设置签名
        String sign = AesUtils.sign(data, "789789");
        jdResDto1.setSysSign(sign);
        return JSON.toJSONString(jdResDto1);
    }

    @Override
    public String exceptionProcess(String reqBody) {
        // FIXME 根据异常返回结果
        JdResDto jdResDto = new JdResDto();
        JdReqDto jdReqDto = JSON.parseObject(reqBody, JdReqDto.class);
        jdResDto.setResponseNo(jdReqDto.getRequestNo());
        jdResDto.setResponseTime(String.valueOf(System.currentTimeMillis()));
        jdResDto.setCode(JdResDto.ILLEGAL_REQUEST);
        jdResDto.setMsg("验签失败");
        jdResDto.setResponseData("");
        jdResDto.setSysSign("");
        return JSON.toJSONString(jdResDto);

    }

}


